<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>数组新方法</title>
</head>
<body>
  <script>
    /**
     * 1. Array.from()
     * 2. Array.of()
     * 3. new Array()
     * 4. find()
     * 5. findIndex()
     * 6. some()
     * 7. every()
    */

    // 1. Array.from()，将类数组或者可枚举的对象/字符串转换为数组
    function arr () {
      console.log('arguments', Array.from(arguments));
      // Array.from的回调函数就是map函数
      const mapArr = Array.from(arguments, ele => {
        // 可以在回调函数对转换过的数组进行map操作
        return ele * 2;
      })
      console.log('mapArr', mapArr);
    }
  
    arr(1, 3);

    // 转换字符串
    console.log(Array.from('i am coder'));

    // 2. Array.of()，填充相应元素到数组
    console.log('空数组', Array.of());
    console.log('三个数组', Array.of(1, 3, 5));

    // 3. new Array(),根据参数的个数产生数组
    console.log(new Array(2).fill(1)); // 一个参数时，代表产生数组的长度
    console.log(new Array(4, 5, 6, false, 0, undefined, null, NaN)); // 多个参数时，代表产生数组的元素

    const testArr = [1, 2, 3, 6, 0];
    // 4. find()
    const findItem = testArr.find(item => item > 2);
    console.log('findItem', findItem);

    // 5. findIndex()
    const findIndex = testArr.findIndex(item => item > 2);
    console.log('findIndex', findIndex);

    // 6. some()
    const isSomeLarger3 = testArr.some(item => item > 3);
    console.log('isSomeLarger3', isSomeLarger3);

    // 7. every()
    const isEveryLarger3 = testArr.every(item => item > 3);
    console.log('isEveryLarger3', isEveryLarger3);
  </script>
</body>
</html>